﻿package com.winmastergames.lolgame {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.*;
	import com.winmastergames.lolgame.engine;
	import flash.media.Sound;
	import flash.utils.Timer;
	import com.winmastergames.lolgame.gameUtils;
	import com.winmastergames.lolgame.globals;

	/**
	 * ...
	 * @author Taylor
	 */
	public class turret extends MovieClip {
		var enemyIndex = 0;
		var fireTimer:Timer = new Timer(900);
		var active:Boolean = false;
		var goToMouse:Boolean = false;
		var buying:Boolean = false;
		var mStage:mapDefault;
		var killed:Boolean = false;
		var snd_gun_shot:Sound = new pistol_fire_snd();
		var turretBase:turret_base = new turret_base();

		public function turret(stageRef:mapDefault, X:Number, Y:Number, turretActive:Boolean, buyingTurret:Boolean){
			mStage = stageRef;
			stageRef.turrets.addChild(turretBase);
			stageRef.turrets.addChild(this);
			if (buyingTurret == false){
				this.x = X;
				this.y = Y;
				turretBase.x = X;
				turretBase.y = Y;
			} else {
				buying = buyingTurret;
				goToMouse = true;
			}
			active = turretActive;
			addEventListener(Event.ENTER_FRAME, loop);
			fireTimer.start();
			fireTimer.addEventListener(TimerEvent.TIMER, fireShot);
		}

		public function getBuying(){
			return (buying);
		}

		function fireShot(e:Event){
			fireTimer.stop();
			if (killed == false && engine.objectsPaused == false){
				fireTimer.reset();
				if (engine.st_enemyArray.length > 0 && active == true){
					gameUtils.makeBullet(this, engine.st_level_mc, stage, engine.st_bulletArray, x, y, 100);
					snd_gun_shot.play();
				}
				fireTimer.start();
			}
		}

		function convert(radians:Number):Number {
			var degrees = radians * (180 / Math.PI);
			return degrees;
		}

		function killSelf(){
			mStage.turrets.removeChild(this)
			mStage.turrets.removeChild(turretBase);
			killed = true;
		}

		function loop(e:Event){
			if (killed == false && engine.objectsPaused == false){
				if (goToMouse == true){
					var X:int = stage.mouseX - engine.st_level_mc.x;
					var Y:int = stage.mouseY - engine.st_level_mc.y;
					x = X;
					y = Y;
					turretBase.x = X;
					turretBase.y = Y;
				}
				var distance:Number = 9999999999;
				for (var i:Number = 0; i < engine.st_enemyArray.length; i++){
					if (globals.getDistance(x, y, engine.st_enemyArray[i].x, engine.st_enemyArray[i].y) < distance){
						enemyIndex = i;
						distance = globals.getDistance(x, y, engine.st_enemyArray[i].x, engine.st_enemyArray[i].y);
					}
				}
				if (engine.st_enemyArray.length > 0 && active == true){
					//current direction vector
					var v1:Object = new Object();
					v1.vx = Math.cos(rotation / 180 * Math.PI);
					v1.vy = Math.sin(rotation / 180 * Math.PI);
					//vector to target point
					var v2:Object = new Object();
					v2.vx = engine.st_enemyArray[enemyIndex].x - x;
					v2.vy = engine.st_enemyArray[enemyIndex].y - y;
					//dot product between target vector and left hand normal of current vector
					var dp:Number = v1.vy * v2.vx - v1.vx * v2.vy;
					var rotAmount:Number = 5;
					if (dp > 1){
						rotation -= rotAmount;
					} else if (dp < -1){
						rotation += rotAmount;
					} else if (v1.vx * v2.vx + v1.vy * v2.vy > 0){
						//snap into taget angle if we are facing in correct direction
						rotation = Math.atan2(v2.vy, v2.vx) / Math.PI * 180;
					}
				}
			}
		}
	}
}